//
//  MoreViewController.m
//  美食百科
//
//  Created by dllo on 16/2/1.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "MoreViewController.h"
#import "CollectViewController.h"
#import "WebViewController.h"
#import <MessageUI/MessageUI.h>
#import "SDImageCache.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface MoreViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSArray *titles;
@end

@implementation MoreViewController
- (void)dealloc
{
    [_tableView release];
    [_titles release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//self.view.backgroundColor = [UIColor whiteColor];

self.navigationController.navigationBar.tintColor = [UIColor grayColor];
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.translucent = NO;

UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
UIBarButtonItem *leftItemSecond = [[UIBarButtonItem alloc]initWithTitle:@"更多"style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItems = @[leftItemFirst,leftItemSecond];

    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
    
    //日间为NO,夜间为YES
    BOOL night = [userDefaults boolForKey:@"夜间模式"];
    
    if (!night) {
        NSLog(@"改为夜间");
        
        self.titles = @[@"我的收藏",@"点击开启夜间模式",@"清除缓存",@"常见食物的GI值"];
        
    }else
    {
        self.titles = @[@"我的收藏",@"点击开启日间模式",@"清除缓存",@"常见食物的GI值"];
    }
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"more"];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"more"];
    cell.textLabel.text = self.titles[indexPath.row];
    cell.textLabel.textColor = self.color;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    
   // cell.textLabel.textColor = self.color;

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CollectViewController *collect= [[CollectViewController alloc]init];
    collect.item = self.titles[indexPath.row];
    [self.navigationController pushViewController:collect animated:YES];
    
    [collect release];
    }else if (indexPath.row == 1){
        NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
        
        //日间为NO,夜间为YES
        BOOL night = [userDefaults boolForKey:@"夜间模式"];
        
        if (!night) {
            NSLog(@"改为夜间");
            self.titles = @[@"我的收藏",@"点击开启日间模式",@"清除缓存",@"常见食物的GI值"];
            [self.tableView reloadData];
            [userDefaults setBool:YES forKey:@"夜间模式"];
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"夜间",@"Mode", nil];
            //发送通知
            [[NSNotificationCenter defaultCenter]postNotificationName:@"模式" object:self userInfo:dic];
        }else
        {
            NSLog(@"改为白天");
            self.titles = @[@"我的收藏",@"点击开启夜间模式",@"清除缓存",@"常见食物的GI值"];
            [self.tableView reloadData];

            [userDefaults setBool:NO forKey:@"夜间模式"];
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"白天",@"Mode", nil];
            //发送通知
            [[NSNotificationCenter defaultCenter]postNotificationName:@"模式" object:self userInfo:dic];
            
        }
        
        [self.tableView reloadData];
        
    }else if (indexPath.row == 2){
        
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        //缓存的大小
        CGFloat fileSize = [self folderSizeAtPath:cachePath];
        [self creatAlertController:fileSize];
        
    }else if (indexPath.row == 3){
        WebViewController *webView = [[WebViewController alloc]init];
        
        [self.navigationController pushViewController:webView animated:YES];
    }
    
    
    
}
-(void)creatAlertController:(CGFloat)fileSize
{
    //初始化alertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否清理缓存" message:[NSString stringWithFormat:@"缓存大小为%.2fMB",fileSize] preferredStyle:UIAlertControllerStyleAlert];
    
    //两个按钮的事件
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //清理缓存
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
        NSLog(@"files :%lu",(unsigned long)[files count]);
        for (NSString *p in files) {
            NSError *error;
            NSString *path = [cachPath stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            }
        }
        
        [[SDImageCache sharedImageCache] cleanDisk];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark 计算缓存大小
-(CGFloat)folderSizeAtPath:(NSString *)folderPath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) {
        return 0;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString *fileName = nil;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return (folderSize + [[SDImageCache sharedImageCache] getSize])/(1024.0*1024.0);
}

- (long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
    
}

-(void)backAction:(UIBarButtonItem *)item{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;

    
}
- (BOOL)prefersStatusBarHidden{
    NSLog(@"ne");
    return NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
